import os
ENV = Environment(MSVS_VERSION = '9.0',MSVC_VERSION = '9.0')
#print ENV.Dump()
#--------------------------------------------------------------------------------------------------------------

AddOption("-z", "--vcproj", action="store_true", dest="build_vcproj", default=False, help="Generate Visual Studio project. Default: don't.")

#--------------------------------------------------------------------------------------------------------------

# use optimum number of cpus
num_cpu = int(os.environ.get('NUMBER_OF_PROCESSORS', 2))
SetOption('num_jobs', num_cpu)
print "running with ", GetOption('num_jobs'), "jobs."

#--------------------------------------------------------------------------------------------------------------
# create visual studio project file
#--------------------------------------------------------------------------------------------------------------

vsc_accept_suffixes = [ '.cpp', '.c', '.asm', '.h', '.inl', '.hpp', '.s', '.inc', '.mm', '.m', '.txt', 'SConscript', '.bat', '.sh' ]

def dirwalk(dir, giveDirs=0):
    """
    walk a directory tree, using a generator
    """
    import os

    for f in os.listdir(dir):
        fullpath = os.path.join(dir,f)
        if os.path.isdir(fullpath) and not os.path.islink(fullpath):
            if not len(os.listdir(fullpath)):
                yield fullpath + os.sep
            else:
				if fullpath.find('.hg') == -1:
						for x in dirwalk(fullpath):  # recurse into subdir
								if os.path.isdir(x):
										if giveDirs:
												yield x
								else:
										yield x
        else:
			for suff in vsc_accept_suffixes:
					if fullpath.endswith( suff ) and fullpath.find('boost') == -1:
							yield fullpath

#-------------------------------------------------------------------------------

EXECUTABLE_NAME = "ExrConverter"

LIB_DIRS = [
	"../FreeImage/Dist/",
]

LIBS = [
	"FreeImage"
]

INC_DIRS = [
	os.path.abspath( "../../FreeImage/Dist/" ),
]

CPP_FLAGS = [] 
CPP_FLAGS.append ( '/EHsc' )
CPP_FLAGS.append ( '/nologo' )
CPP_FLAGS.append ( '/Zi' )                      # procude full debug information
CPP_FLAGS.append ( '/DEBUG:Yes' )
CPP_FLAGS.append ( '/INCREMENTAL:NO' )

LINK_FLAGS = []
LINK_FLAGS.append ( '/nologo' )
LINK_FLAGS.append ( '/DEBUG' )
LINK_FLAGS.append ( '/INCREMENTAL:NO' )
LINK_FLAGS.append ( '/SUBSYSTEM:CONSOLE' )

CPP_DEFS = {}
CPP_DEFS[ '_DEBUG'               ]       =       "1"
CPP_DEFS[ 'DEBUG'               ]       =       "1"
CPP_DEFS[ 'BUILD_WIN_SDL'       ]       =       "1"
CPP_DEFS[ '__WIN32__'           ]       =       "1"

#-------------------------------------------------------------------------------

BUILD_CACHE_DIR = os.path.abspath('./')
Export('ENV')
Export('LIBS')
Export('LIB_DIRS')
Export('INC_DIRS')
Export('EXECUTABLE_NAME')
Export('BUILD_CACHE_DIR')
Export('CPP_FLAGS')
Export('LINK_FLAGS')
Export('CPP_DEFS')

#-------------------------------------------------------------------------------

if GetOption('build_vcproj'):
	vsc_sources = []
	print( 'Collecting files for Visual Studio project...')
	for s in dirwalk('../../'):
		vsc_sources.append ( s )
	proj = ENV.MSVSProject(target = 'ExrCoverter' + ENV['MSVSPROJECTSUFFIX'], srcs = vsc_sources, variant = 'Debug')
	Default ( proj )
else:
	ENV.SConscript("../main.scons", duplicate = 0  )

#-------------------------------------------------------------------------------
